package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/user/")
@Tag(name="用户管理")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    //添加用户功能
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsResult addUser(@RequestBody @Validated UserAddParam userAddParam){

        System.out.println(userAddParam);
        User user=new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return new JsResult(StatusCode.OPERATION_SUCCESS,"添加成功");
    }
    //查询用户功能
    @Operation(summary = "查询用户列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("list")
    public JsResult listUser(Integer id){
        List<UserListVO> list = userMapper.selectUser(id);
        return new JsResult(StatusCode.OPERATION_SUCCESS,list);

    }
    //删除
    @Operation(summary = "删除用户")
    @ApiOperationSupport(order = 30)
    @PostMapping("del/{id}")
    public JsResult deleteUser(@PathVariable @Range(min=1,max=100,message = "id范围1-100") Integer id){
        int rows =userMapper.deleteUser(id);
        if (rows>0){
            return new JsResult(StatusCode.OPERATION_SUCCESS,"删除成功");
        }else {
            return new JsResult(StatusCode.OPERATION_FAILED,"删除失败");
        }

    }
    //更新
    @Parameters(value = {
            @Parameter(name="id",required = true),
            @Parameter(name="title")

    })
    @Operation(summary = "更新用户")
    @ApiOperationSupport(order = 40)
    @PostMapping("update")
    public JsResult updateUser(UserUpdateParam userUpdateParam){
        System.out.println(userUpdateParam);
        User user=new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int rows=userMapper.updateUserById(user);
        if(rows>0){
            return new JsResult(StatusCode.OPERATION_SUCCESS,"更新成功");
        }else{
            return new JsResult(StatusCode.OPERATION_FAILED,"更新失败");
        }
    }

}
